<template>
    <div id="login">
        <div id="loginBox">
            <div style="width: 400px;background-color: white;margin: 200px auto;padding: 50px 20px 2px 10px;border: 0px solid;border-radius: 25px">

                <el-form :model="loginForm" status-icon :rules="loginRules" ref="ruleForm" label-width="80px"
                         class="demo-ruleForm" id="loginForm">
                    <el-form-item label="账号:" prop="nikename">
                        <el-input type="text" v-model="loginForm.nikename" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="密码:" prop="password">
                        <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="login()">提交</el-button>
                        <el-button>重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                loginForm: {nikename: "", password: ""},
                //登录的规则
                loginRules: {
                    nikename: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ]
                }

            }
        },
        methods: {
            login(){
                this.$http.post("http://localhost:8081/login",this.loginForm).then(resp=>{
                    if(resp.data.code===200){
                        //获取服务器响应的token值。保存LocalStorage
                        localStorage.setItem("token",resp.data.data);
                        //跳转路由 不能使用location.href
                        this.$router.push("/student")
                    }else{
                        this.$message.error("账户或密码错误")
                    }
                })
            }
        }
    }
</script>


<style>
    * {
        margin: 0;
        padding: 0;
    }

    #loginBox {
        width: 100vw;
        height: 100vh;
        background-color: #f0f7ff;
        overflow: hidden;


    }
</style>